<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Card as CardModel;
use app\admin\model\Job as JobModel;
use app\admin\model\Company as CompanyModel;
use app\admin\model\Log as LogModel;
use app\admin\model\Info as InfoModel;
use app\admin\model\Group as GroupModel;
use app\admin\model\Depart as DepartModel;
use EasyWeChat\Factory;

class Card extends Controller
{
    protected $CardModel;
    protected $JobModel;
    protected $CompanyModel;
    protected $LogModel;
    protected $InfoModel;
    protected $GroupModel;
    protected $DepartModel;

    public function __construct()
    {
        if (!session('adminId')) {
            $this->redirect('login/index');
        }
        $this->CardModel = new CardModel();
        $this->JobModel = new JobModel();
        $this->CompanyModel = new CompanyModel();
        $this->LogModel = new LogModel();
        $this->InfoModel = new InfoModel();
        $this->GroupModel = new GroupModel();
        $this->DepartModel = new DepartModel();
        $this->gid = session('adminGid');
    }

    public function index()
    {
        $gid = session('adminGid');
        $query = [
            'ispage' => true,
            'where' => [
                'gid' => $this->gid,
            ],
            'order' => [
                'is_top' => 'desc',
                'create_time' => 'desc',
            ],
        ];
        $cardRes = $this->CardModel->getLst($query);

        return view('index', [
            'card' => $cardRes,
        ]);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data['gid'] = $this->gid;
            $res = $this->CardModel->save($data);
            if ($res) {
                $this->success('新增成功', 'card/index');
            } else {
                $this->error('新增失败,请检查重试');
            }
        }

        $query = [
            'where' => [
                'gid' => $this->gid,
            ],
            'order' => [
                'update_time' => 'desc',
            ],
        ];
        $jobRes = $this->JobModel->getLst($query);
        $companyRes = $this->CompanyModel->getLst($query);
        $departRes = $this->DepartModel->getLst($query);

        return view('add', [
            'job' => $jobRes,
            'company' => $companyRes,
            'depart' => $departRes,
        ]);
    }

    public function edit($id, $did = 0)
    {
        $cardRes = $this->CardModel->get($id);
        if (request()->isPost()) {
            $data = input();
            $data['gid'] = $this->gid;
            $res = $cardRes->save($data);
            if ($res) {
                $this->success('编辑成功', 'card/index');
            } else {
                $this->error('编辑失败,请检查重试');
            }
        }

        $query = [
            'where' => [
                'gid' => $this->gid,
            ],
            'order' => [
                'update_time' => 'desc',
            ],
        ];
        $jobQuery = [
            'where' => [
                'did' => $did,
                'gid' => $this->gid,
            ],
            'order' => [
                'update_time' => 'desc',
            ],
        ];
        $jobRes = $this->JobModel->getLst($jobQuery);
        $companyRes = $this->CompanyModel->getLst($query);
        $departRes = $this->DepartModel->getLst($query);

        return view('edit', [
            'card' => $cardRes,
            'job' => $jobRes,
            'company' => $companyRes,
            'depart' => $departRes,
        ]);
    }

    public function del($id)
    {
        if (request()->isPost()) {
            $res = $this->CardModel->destroy($id);
            if ($res) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    public function disabled($id, $type)
    {
        if (request()->isPost()) {
            $cardRes = $this->CardModel->get($id);
            $cardRes->is_disabled = $type;
            $res = $cardRes->save();
            if ($res) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    public function top($id, $type)
    {
        if (request()->isPost()) {
            $cardRes = $this->CardModel->get($id);
            $cardRes->is_top = $type;
            $res = $cardRes->save();
            if ($res) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    public function getCode($id)
    {
        $cardRes = $this->CardModel->get($id);
        $gid = session('adminGid');
        $infoRes = $this->InfoModel->where('gid', $gid)->find();
        $adminLevel = session('adminLevel');
        if ($adminLevel > 0) {
            if ($infoRes['wechatappid'] || $infoRes['wechatsecret']) {
                $config = [
                    'app_id' => $infoRes['wechatappid'],
                    'secret' => $infoRes['wechatsecret'],
                ];
            } else {
                $this->error('你尚未设置APPID与SECRET,请检查重试');
            }
        } else {
            $config = [
                'app_id' => config('wechat.mini_program.default.app_id'),
                'secret' => config('wechat.mini_program.default.secret'),
            ];
        }
        $weChatApp = Factory::miniProgram($config);
        $path = '/pages/my/index?id='.$id.'&type=1';
        $directoryCwd = getcwd();
        $response = $weChatApp->app_code->get($path);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs($directoryCwd.'/codeimg', 'comma'.$cardRes['id'].'appcode.png');
        }

        return view('code', [
            'code' => $filename,
            'name' => $cardRes->name,
        ]);
    }

    public function codeAuth()
    {
        $gid = session('adminGid');
        $infoRes = $this->InfoModel->where('gid', $gid)->find();
        $adminLevel = session('adminLevel');
        if ($adminLevel > 0) {
            if ($infoRes['wechatappid'] || $infoRes['wechatsecret']) {
                $config = [
                    'app_id' => $infoRes['wechatappid'],
                    'secret' => $infoRes['wechatsecret'],
                ];
            } else {
                $this->error('你尚未设置APPID与SECRET,请检查重试');
            }
        } else {
            $config = [
                'app_id' => config('wechat.mini_program.default.app_id'),
                'secret' => config('wechat.mini_program.default.secret'),
            ];
        }
        $weChatApp = Factory::miniProgram($config);
        $path = '/pages/my/home';
        $directoryCwd = getcwd();
        $response = $weChatApp->app_code->get($path);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs($directoryCwd.'/codeimg', 'commag'.$gid.'appcode.png');
        }

        return view('codeauth', [
            'code' => $filename,
        ]);
    }

    public function getCodeImg($id)
    {
        $image = \think\Image::open('./code.png');
        // 给原图左上角添加水印并保存water_image.png
        $image->water('logo.png', [300, 200], 50)->text('will的名片', 'fzfsk.ttf', 20, '#00000', [300, 400])->save('text_image.png');

        return view('code2');
    }

    public function search()
    {
        $val = input('seacrchval');
        $cardRes = $this->CardModel->where('gid', $this->gid)->where('name|name_en|phone|email|wechat', 'like', $val.'%')->order('is_top', 'desc')->paginate(12, false, [
            'query' => [
                'seacrchval' => $val,
            ],
        ]);

        return view('search', [
            'seacrchval' => $val,
            'card' => $cardRes,
        ]);
    }

    public function company($id, $name = 0)
    {
        $query = [
            'ispage' => true,
            'limit' => 12,
            'where' => [
                'company_id' => $id,
                'gid' => $this->gid,
            ],
            'order' => [
                'is_top' => 'desc',
                'create_time' => 'desc',
            ],
        ];
        $cardRes = $this->CardModel->getLst($query);

        return view('filter', [
            'card' => $cardRes,
            'name' => $name,
        ]);
    }

    public function job($id, $name = 0)
    {
        $query = [
            'ispage' => true,
            'limit' => 12,
            'where' => [
                'job_id' => $id,
                'gid' => $this->gid,
            ],
            'order' => [
                'is_top' => 'desc',
                'create_time' => 'desc',
            ],
        ];
        $cardRes = $this->CardModel->getLst($query);

        return view('filter', [
            'card' => $cardRes,
            'name' => $name,
        ]);
    }

    public function depart($id, $name = 0)
    {
        $query = [
            'ispage' => true,
            'limit' => 12,
            'where' => [
                'did' => $id,
                'gid' => $this->gid,
            ],
            'order' => [
                'is_top' => 'desc',
                'create_time' => 'desc',
            ],
        ];
        $cardRes = $this->CardModel->getLst($query);

        return view('filter', [
            'card' => $cardRes,
            'name' => $name,
        ]);
    }

    public function log($id)
    {
        $data = [
            'cid' => $id,
        ];
        $cardRes = $this->CardModel->get($id);
        $logRes = $this->LogModel->where($data)->order('id', 'desc')->paginate(12);

        return view('log', [
            'log' => $logRes,
            'card' => $cardRes,
        ]);
    }
}
